#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Windows;
using Amarok.Agents;


namespace Amarok.Presentation
{
	/// <summary>
	/// This type represents a Window Description, which is a specific View Description for top-level windows 
	/// and dialogs. A View Description is a description of the combination of view, view model and associated 
	/// controller. A view description is capable to instantiate and connect view and controller and to manage 
	/// the lifetimes of these instances.
	/// </summary>
	/// 
	/// <typeparam name="TWindow">
	/// The type of window in this combination of view, view model and controller.</typeparam>
	/// <typeparam name="TController">
	/// The type of controller in this combination of view, view model and controller.</typeparam>
	/// <typeparam name="TViewModel">
	/// The type of view model in this combination of view, view model and controller.</typeparam>
	public class WindowDescription<TWindow, TController, TViewModel> : WindowDescription
		where TWindow : Window, IView, new()
		where TController : Controller<TViewModel>
		where TViewModel : new()
	{
		#region ++ Public Interface ++

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		public WindowDescription()
			: base(typeof(TWindow), typeof(TController))
		{
		}

		#endregion

		#region Implementation

		/// <summary>
		/// This overrideable method is called to create the view instance.
		/// </summary>
		/// 
		/// <param name="environment">
		/// The agent environment for this controller.</param>
		/// <param name="options">
		/// The agent options for this controller.</param>
		/// 
		/// <returns>
		/// A reference to the newly created view instance.</returns>
		protected override IView CreateView(AgentEnvironment environment, AgentOptions options)
		{
			var view = new TWindow();
			Amarok.Presentation.View.SetEnvironment(view, environment);
			Amarok.Presentation.View.SetOptions(view, options);
			return view;
		}

		/// <summary>
		/// This overrideable method is called to create the controller instance.
		/// </summary>
		/// 
		/// <param name="environment">
		/// The agent environment for this controller.</param>
		/// <param name="options">
		/// The agent options for this controller.</param>
		/// 
		/// <returns>
		/// A reference to the newly created controller instance.</returns>
		protected override IController CreateController(AgentEnvironment environment, AgentOptions options)
		{
			return Amarok.Presentation.Controller.Factory.SpawnController<TController, TViewModel>(
				base.View,
				new TViewModel(),
				environment,
				options);
		}

		/// <summary>
		/// This overrideable method is called to create the controller instance.
		/// </summary>
		/// 
		/// <param name="environment">
		/// The agent environment for this controller.</param>
		/// <param name="options">
		/// The agent options for this controller.</param>
		/// <param name="viewModel">
		/// The view model that is used as intermediate between controller and view.</param>
		/// 
		/// <returns>
		/// A reference to the newly created controller instance.</returns>
		protected override IController CreateController(AgentEnvironment environment, AgentOptions options, Object viewModel)
		{
			return Amarok.Presentation.Controller.Factory.SpawnController<TController, TViewModel>(
				base.View,
				(TViewModel)viewModel,
				environment,
				options);
		}

		#endregion

	}
}
